/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean;

import com.aptech.entities.TblComplaint;
import com.aptech.mBean.util.PaginationHelper;
import com.aptech.sBean.TblComplaintFacade;
import java.io.Serializable;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author LanhAnh
 */
@ManagedBean(name = "complaintController")
@SessionScoped
public class ComplaintController implements Serializable {

    @EJB
    private TblComplaintFacade tblComplaintFacade;
    private TblComplaint current;
    private DataModel items = null;
    private PaginationHelper paginationHelper;
    private int selectedItemIndex;

    /**
     * Creates a new instance of ComplaintController
     */
    public ComplaintController() {
    }

    public TblComplaint getSelected() {
        if (current == null) {
            current = new TblComplaint();
            selectedItemIndex = -1;
        }
        return current;
    }

    private TblComplaintFacade getFacade() {
        return tblComplaintFacade;
    }

    public PaginationHelper getPaginationHelper() {
        if (paginationHelper == null) {
            paginationHelper = new PaginationHelper(10) {
                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return paginationHelper;
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPaginationHelper().createPageDataModel();
        }
        return items;
    }

    public String prepareCreate() {
        current = new TblComplaint();
        selectedItemIndex = -1;
        return "ComplaintManagement";
    }

    public String create() {
        try {
            getFacade().create(current);
            //  JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/i18n/Bundle").getString("ProjectCreated"));
            return prepareCreate();
        } catch (Exception e) {
            // JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/i18n/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareList() {
        recreateModel();
        return "ComplaintManagement";
    }

    private void recreateModel() {
        items = null;
    }

    private void recreatePagination() {
        paginationHelper = null;
    }

    public void onEdit(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edit Complaint", ((TblComplaint) e.getObject()).getCpID().toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        getFacade().edit((TblComplaint) e.getObject());
    }

    public void onEditCancel(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edit Complaint Cancelled", ((TblComplaint) e.getObject()).getCpID().toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @FacesConverter(forClass = TblComplaint.class)
    public static class UserControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ComplaintController controller = (ComplaintController) context.getApplication().getELResolver().
                    getValue(context.getELContext(), null, "complaintController");
            return controller.tblComplaintFacade.find(getKey(value));
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof TblComplaint) {
                TblComplaint o = (TblComplaint) object;
                return getStringKey(o.getCpID());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TblComplaint.class.getName());
            }
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }
    }
    //Statistic
    private Date dateStart = new Date(110, 01, 01);
    private Date dateEnd = new Date();
    private int dateType = 0;

    public int getDateType() {
        return dateType;
    }

    public void setDateType(int dateType) {
        this.dateType = dateType;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }
    private DataModel itemsStt = null;


    public DataModel getItemsStt() {
        if (itemsStt == null) {
            if (dateType == 0) {
                itemsStt = sttGetByDateCreated();
            } else {
                itemsStt = sttGetByDateAdjustmented();
            }
        }
        return itemsStt;
    }

    public DataModel prepareItemsStt() {
        if (dateType == 0) {
            itemsStt = sttGetByDateCreated();
        } else {
            itemsStt = sttGetByDateAdjustmented();
        }
        return itemsStt;
    }

    public void setItemsStt(DataModel itemsStt) {
        this.itemsStt = itemsStt;
    }

    public DataModel sttGetByDateCreated() {

        itemsStt = new ListDataModel(getFacade().getTblComplaintByDateCreated(dateStart, dateEnd));
        return itemsStt;
    }

    public DataModel sttGetByDateAdjustmented() {

        itemsStt = new ListDataModel(getFacade().getTblComplaintByDateAdjustmented(dateStart, dateEnd));
        return itemsStt;
    }

    public DataModel sttGetOrderHasManyComplaint() {
        itemsStt = new ListDataModel(getFacade().getTblComplaintByOrderHasManyComplaint(dateStart, dateEnd));
        return itemsStt;
    }

    public String sttView(TblComplaint c) {
        current = c;
        return "/Admin/Complaint/ViewComplaint";
    }
}
